package modulos;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import auxiliary.Constants;
import auxiliary.Database;

public class TableModelMaquinaria extends AbstractTableModel {

	Vector cache;
	String[] headers;
	int colCount;

	Database db;

	TableModelMaquinaria() {
		cache = new Vector();
	}

	public void updateTable() {
		db = new Database();
		
		cache = new Vector();
		ArrayList<ArrayList<String>> maquinariaData = new ArrayList<ArrayList<String>>();

		maquinariaData = db.retrieveMaquinaria();
		db.closeConnection();
		headers = new String[] {"Modelo", "Prod" , "Llen" , "Lot" , "Cap",
				"#", "Funcionarios Asignados" };
		colCount = headers.length;
		
		for (int i = 0; i < maquinariaData.size(); i++) {
			String[] list = (Constants.arrayListToString(maquinariaData))[i];
			list = Arrays.copyOfRange(list, 1, list.length);
			cache.addElement(list);
		}

		fireTableChanged(null);

	}

	@Override
	public int getColumnCount() {
		return colCount;
	}

	@Override
	public int getRowCount() {
		return cache.size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		return ((String[]) cache.elementAt(row))[col];
	}

	public String getColumnName(int i) {
		return headers[i];
	}

}